﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SSP;

namespace SSPDemo
{
    class SSPMenuItem : SSPNode
    {
        public Texture2D texture;
        int rows, columns, totalFrames;
        public readonly int width;
        public readonly int height;
        public int currentFrame;
        public double animationSpeed;
        public bool looping = true;
        double currentSpeedVar = 0;
        bool isSelected;
        bool isActivated;
        public Color color;

        public SSPMenuItem(string textureName, int w, int h)
        {
            texture = SSPDirector.Instance.gameInstance.Content.Load<Texture2D>(textureName);

            rows = 2;
            columns = 1;
            totalFrames = 2;

            width = w;
            height = h;

            anchorPoint.X = width / 2;
            anchorPoint.Y = height / 2;

            position.X = 0;
            position.Y = 0;

            color = Color.White;
            currentFrame = 0;

            animationSpeed = (double)MathHelper.Clamp(.1f, 0, 1);

            this.unselected();
        }

        public void activate()
        {
            isActivated = true;
            isSelected = false;
        }

        public void selected()
        {
            isActivated = false;
            isSelected = true;
        }
        
        public void unselected()
        {
            isActivated = false;
            isSelected = false;
        }

        public override void draw()
        {
            if (isSelected)
            {

                SSPDirector.Instance.spriteBatchInstance.Draw(texture,
                new Rectangle((int)absolutePosition.X, (int)absolutePosition.Y, (int)(width * scale.X), (int)(height * scale.Y)),
                new Rectangle(width, (currentFrame / columns) * height, width, height),
                color, rotation, anchorPoint, SpriteEffects.None, zOrder);
                //Console.WriteLine("Drawing Animated Sprite");
            }
            else if (isActivated)
            {
                SSPDirector.Instance.spriteBatchInstance.Draw(texture,
                new Rectangle((int)absolutePosition.X, (int)absolutePosition.Y, (int)(width * scale.X), (int)(height * scale.Y)),
                new Rectangle(0, height, width, height),
                color, rotation, anchorPoint, SpriteEffects.None, zOrder);
                //Console.WriteLine("Drawing Static Sheet Sprite - {0},{1},{2},{3}", sourceRect.X, sourceRect.Y, sourceRect.Width, sourceRect.Height);
            }
            else
            {
                SSPDirector.Instance.spriteBatchInstance.Draw(texture,
                new Rectangle((int)absolutePosition.X, (int)absolutePosition.Y, (int)(width * scale.X), (int)(height * scale.Y)),
                new Rectangle(0, 0, width, height),
                color, rotation, anchorPoint, SpriteEffects.None, zOrder);
            }

            base.draw();
        }

        public override void update()
        {
            if (currentSpeedVar >= 1)
            {
                currentFrame++;

                currentSpeedVar = (1 - currentSpeedVar) + animationSpeed;
            }
            else
            {
                currentSpeedVar += animationSpeed;
            }

            if (currentFrame == totalFrames)
            {
                if (looping == true)
                {
                    currentFrame = 0;
                }
            }
            base.update();
        }

    }
}
